package com.hfut.jpoa.action;

import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.DateUtils;
import com.hfut.jpoa.common.util.UID;
import com.hfut.jpoa.dto.ProviderDTO;
import com.hfut.jpoa.model.Provider;
import com.hfut.jpoa.service.ProviderService;
import com.hfut.jpoa.service.exception.ProviderException;
import com.sun.tools.javac.util.List;

public class ProviderAction extends AbsAction<Provider> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7610206122538973881L;

	private ProviderService providerService;

	private Provider provider;

	private List<Provider> lstProviders;

	/**
	 * 包含分页接受前台变量
	 */
	private ProviderDTO providerDTO;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 增加
	 */
	public final static String ACTIONTYPE_ADD = "0";

	/**
	 * 修改
	 */
	public final static String ACTIONTYPE_UPDATE = "1";

	/**
	 * 详情
	 */
	public final static String ACTIONTYPE_DETAIL = "2";

	/**
	 * 分页结果集
	 */
	private ResultDto<Provider> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	public String initProvider() throws ProviderException {
		ProviderDTO categoryDTO = new ProviderDTO();
		Pagination pagination = new Pagination(0);
		categoryDTO.setPagination(pagination);

		if (null == provider) {
			provider = new Provider();
		}

		categoryDTO.setProvider(provider);

		resultDto = providerService.queryCategoryInfoForPage(categoryDTO);
		totalPage = resultDto.getPagination().getTotalPage();

		return "provider_list_init";
	}

	/**
	 * 修改供应商表
	 * 
	 * @return
	 * @throws ProviderException
	 */
	public String editProvider() throws ProviderException {
		try {
			providerService.updateProvider(provider);
		} catch (ProviderException e) {
			e.printStackTrace();
		}

		return "provider_list_init";
	}

	/**
	 * 显示列表
	 * 
	 * @return
	 */
	public String list() throws ProviderException {

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");

		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (providerDTO == null) {
			providerDTO = new ProviderDTO();
		}

		providerDTO.setPagination(pagination);
		resultDto = providerService.list(providerDTO);
		totalPage = resultDto.getPagination().getTotalPage();

		return "list";
	}

	/**
	 * 增加页面初始化
	 */
	public String providerAddInit() throws ProviderException {
		actionType = ACTIONTYPE_ADD;

		return "provider_add_init";
	}

	/**
	 * 修改页面初始化
	 */
	public String providerUpdateInit() throws ProviderException {
		actionType = ACTIONTYPE_UPDATE;
		Provider resultProvider = providerService.findProviderInfo(provider);
		provider = resultProvider;

		return "provider_add_init";
	}

	/**
	 * 新增供应商配置
	 * 
	 * @return
	 */
	public String addProvider() throws ProviderException {
		provider.setId(UID.getID());
		// provider.setCreator(creator);
		try {
			provider.setCreateDate(DateUtils.getNowDate());
		} catch (ParseException e) {
			e.printStackTrace();
		}

		providerService.saveProvider(provider);

		return "provider_list_init";
	}

	public Provider getProvider() {
		return provider;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

	public List<Provider> getLstProviders() {
		return lstProviders;
	}

	public void setLstProviders(List<Provider> lstProviders) {
		this.lstProviders = lstProviders;
	}

	public ProviderDTO getProviderDTO() {
		return providerDTO;
	}

	public void setProviderDTO(ProviderDTO providerDTO) {
		this.providerDTO = providerDTO;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public ResultDto<Provider> getResultDto() {
		return resultDto;
	}

	public void setResultDto(ResultDto<Provider> resultDto) {
		this.resultDto = resultDto;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public void setProviderService(ProviderService providerService) {
		this.providerService = providerService;
	}

}
